<template>
    <Card>
        <div>
            <Affix :offset-top="120">
                <div class="contract-detial-btns" v-if="isApprove">
                    <Button type="primary" @click="back">返回</Button>
                    <Button type="primary" v-if="!this.Obj.readOnly" @click="checkPenalty('pass')">审核通过</Button>
                    <Button type="primary" v-if="!this.Obj.readOnly" @click="approve('reject')">拒绝</Button>
                </div>
                <div class="contract-detial-btns" v-else>
                    <Button type="primary" @click="back">返回</Button>
                    <Button type="primary" v-if="!this.Obj.readOnly" @click="save">保存</Button>
                    <Button type="primary" v-if="!this.Obj.readOnly" @click="submit">提交</Button>
                </div>
            </Affix>
        </div>
        <Collapse v-model="openPanel">
            <Panel name="1" v-if="isApprove||this.pageParams.processStatus==='approved'">
                日志
                <div class="content" slot="content">
                    <approveHistory :Obj="Obj"/>
                </div>
            </Panel>
            <Panel name="2">
                基本信息
                <div class="content" slot="content">
                    <BaseInfo ref="baseInfo" :formData="pageInfo"/>
                </div>
            </Panel>
            <Panel name="3">
                合同信息
                <div class="content" slot="content">
                    <contractInfo :Obj="Obj"/>
                </div>
            </Panel>
            <Panel name="5">
                业务办理
                <div class="content" slot="content">
                    <Business
                        ref="business"
                        :Obj="Obj"
                        :pageParams="pageInfo"
                        :dataDict="dataDict"
                        :isApprove="isApprove"
                        @saveDataFn="saveDataFn"
                        @submitDataFn="submitDataFn"
                        @childBusinessForm="childBusinessForm"
                        @closeSpin="closeSpin"
                    />
                </div>
            </Panel>
            <Panel name="6" v-if="(!this.Obj.readOnly&&isApprove)||this.pageParams.processStatus==='approved'">
                当前用户: {{userInfo ? userInfo.userRealName :''}}
                <div class="content" slot="content">
                    <ContractCurrentUser ref="contractCurrentUser" :Obj="Obj" @childUserForm="childUserForm"
                                         @closeSpin="closeSpin"/>
                </div>
            </Panel>
        </Collapse>
    </Card>
</template>
<script>
    import moment from "moment";
    import {
        getLimitAmt,
        getCostExempt,
        savePenaltyExempt,
        submitPenaltyExempt,
        approvePenaltyExempt,
    } from "_p/basic/api/contract/fee-waiver/penalty-exempt/penalty-exempt";
    import {findByContractNo} from "_p/basic/api/contract/activated-case/index";
    import {businessSource} from "@/projects/basic/api/contract/channel-type/channel-type";
    // 日志
    import approveHistory from "_p/basic/pages/contract/common/approve-history/approve-history";
    // 业务办理
    import Business from "./Business";
    // 合同信息
    import contractInfo from "_p/basic/pages/contract/common/contract-info/contract-info";
    // 基本信息
    import BaseInfo from "_p/basic/pages/contract/common/base-info";
    // 当前用户
    import ContractCurrentUser from "_p/basic/pages/contract/common/current-user/contractCurrentUser";

    export default {
        components: {
            ContractCurrentUser,
            approveHistory,
            contractInfo,
            BaseInfo,
            Business
        },
        props: {
            Obj: {
                //caseNo, contractNo, roleType
                type: Object,
                required: true
            },
            spinShow: {
                type: Boolean,
                required: true
            },
            // 是否是供应商
            isSP: {
                type: Boolean,
                required: true
            },
            // 是否是供应商
            isLoan: {
                type: String,
                required: true
            },
            //是否是审批页面
            isApprove: {
                type: Boolean,
                required: true
            },
            pageParams: {
                type: Object,
                required: true
            },
            //合同号
            contractNo: {
                type: String,
                required: true
            },
            dataDict: {
                type: Object
            },
            userInfo: {
                type: Object,
                required: true
            },
            roleType: {
                type: Array,
                required: true
            }
        },
        data() {
            return {
                approveType: null,
                limitAmt: null,
                userLimitAmt: null,
                flag: false,
                openPanel: [2, 3, 4, 5, 6],
                loading: true,
                approvalOperating: "", //审批结果
                pageInfo: this.pageParams,
                contractBusiness: ["costExemptBill", "closeContract"],
            };
        },

        mounted() {
            this.init();
        },
        methods: {
            init() {
                if (this.$refs.baseInfo) {
                    this.$refs.baseInfo.formData = Object.assign(
                        {},
                        this.$refs.baseInfo.formData,
                        this.pageInfo
                    );
                }
                this.getCostExempt();
                this.getLimitAmt();
            },
            getCostExempt() {
                getCostExempt().then(res => {
                    if (res.code === "0000") {
                        if (res.data) {
                            this.limitAmt = res.data.limitAmt;
                        }
                    }
                })
            },
            getLimitAmt() {
                getLimitAmt().then(res => {
                    if (res.code === "0000") {
                        if (res.data) {
                            this.userLimitAmt = res.data.limitAmt;
                        }
                    }
                })
            },
            save() {
                if (this.$refs.business) {
                    this.showSpin();
                    this.$refs.business.formValidate("saveDataFn");
                }
            },
            submit() {
                this.$Modal.confirm({
                    title: "确认提交？",
                    onOk: () => {
                        this.findByContractNo({
                            contractBusiness: this.contractBusiness,
                            contractNo: this.contractNo,
                            handleBusiness: ""
                        });
                    }
                });
            },
            findByContractNo(params) {
                findByContractNo(params).then(res => {
                    if (res.code === "0000") {
                        if (res.msg === "success") {
                            if (this.$refs.business) {
                                this.showSpin();
                                this.$refs.business.formValidate("submitDataFn");
                            }
                        }
                    }
                });
            },

            checkPenalty(name) {
                if (this.userLimitAmt && this.$refs.business.formData.exemptionAmt > this.userLimitAmt) {
                    this.$Modal.warning({
                        title: "警告",
                        content: "单笔合同最大豁免金额为：" + this.userLimitAmt + "，您已超额请调整额度",
                    });
                    return;
                }
                if (!this.userLimitAmt) {
                    this.$Modal.warning({
                        title: "警告",
                        content: "无单笔合同最大豁免额度，请设置额度",
                    });
                    return;
                }
                if (this.limitAmt && this.$refs.business.formData.exemptionAmt > this.limitAmt) {
                    this.$Modal.warning({
                        title: "警告",
                        content: "本部门最大豁免金额剩余：" + this.limitAmt + "，您已超额请调整额度",
                    });
                    return;
                }
                if (!this.limitAmt) {
                    this.$Modal.warning({
                        title: "警告",
                        content: "本部门无豁免额度，请设置额度",
                    });
                    return;
                }

                findByContractNo({
                    contractBusiness: this.contractBusiness,
                    contractNo: this.contractNo,
                    handleBusiness: "exemptTypes"
                }).then(res => {
                    if (res.code === "0000") {
                        if (res.msg === "success") {
                            this.approve(name);
                        }
                    }
                })
            },
            // 审核等按钮
            approve(name) {
                if (!name) return;
                let t, val;
                switch (name) {
                    case "pass":
                        t = "审核通过";
                        val = "suggestCheckF";
                        break;
                    case "return":
                        t = "退件";
                        val = "sendBack";
                        break;
                    case "reject":
                        t = "拒绝";
                        val = "suggestRejectF";
                        break;
                }
                this.approveType = name;
                this.approvalOperating = val;
                // 验证 business 组件数据 给下一个方法
                this.$refs.business.formValidate("childBusinessForm");
            },
            // 保存 请求
            saveDataFn(data) {
                if (!data) return;
                let d = this.formatData(data);

                savePenaltyExempt(d)
                    .then(res => {
                        this.$emit("closeSpin");
                        if (res.code === "0000") {
                            this.$Message.success("保存成功!");
                            // if (this.pageInfo.caseNo)
                            this.pageInfo = res.data;
                            // this.$refs.baseInfo.formData = res.data;
                        }
                    })
                    .catch(e => {
                        this.$emit("closeSpin");
                    });
            }
            ,
            //提交 请求
            submitDataFn(data) {
                if (!data) return;
                let d = this.formatData(data);

                submitPenaltyExempt(d)
                    .then(res => {
                        this.$emit("closeSpin");
                        if (res.code === "0000") {
                            this.$Message.success("提交成功!");
                            this.back();
                        }
                    })
                    .catch(e => {
                        this.$emit("closeSpin");
                    });
            }
            ,
            // 审批 请求
            // Business 组件数据
            childBusinessForm(data) {
                if (!data) return;
                let bussiness = this.formatData(data);
                // 验证 contractCurrentUser 组件数据 传给下个方法
                this.$refs.contractCurrentUser.formValidate(
                    "childUserForm",
                    bussiness,
                    this.approveType
                );
            }
            ,
            // contractCurrentUser 组件数据
            childUserForm(data) {
                if (!data) return;
                let d = Object.assign({}, data, {
                    approvalOperating: this.approvalOperating
                });
                let t;
                switch (d.approvalOperating) {
                    case "suggestCheckF":
                        t = "通过";
                        break;
                    case "sendBack":
                        t = "退件";
                        break;
                    case "suggestRejectF":
                        t = "拒绝";
                        break;
                }
                this.$Modal.confirm({
                    title: `确认${t}？`,
                    onOk: () => {
                        this.saveProposalApplicationInfo(d)
                    }
                });
            },
            saveProposalApplicationInfo(d) {
                approvePenaltyExempt(d)
                    .then(res => {
                        this.$emit("closeSpin");
                        if (res.code === "0000") {
                            this.$Message.success("成功!");
                            this.back();
                        }
                    })
                    .catch(e => {
                        this.$emit("closeSpin");
                    });
            }
            ,
            // 格式化
            formatData(data) {
                if (!data) return {};
                this.pageInfo.createTime = null;
                this.pageInfo.updateTime = null;
                let d = Object.assign({}, data, this.pageInfo);
                d.processPerson = this.isLoan;
                d.businessSource = businessSource(this.roleType);
                if (this.isSP) {
                    d.department = d.channelFullName;
                } else {
                    d.department = this.userInfo.userRealName;
                }
                if (d.createTime instanceof Date)
                    d.createTime = moment(d.createTime).format(
                        "YYYY-MM-DD HH:mm:ss"
                    );
                if (d.endDate instanceof Date)
                    d.endDate = moment(d.endDate).format("YYYY-MM-DD HH:mm:ss");
                return d;
            }
            ,
            showSpin() {
                this.$emit("showSpin");
            }
            ,
            closeSpin() {
                this.$emit("closeSpin");
            }
            ,
            back() {
                this.$emit("back");
            }
        }
    }
    ;
</script>
